﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UCL.Powersource;

namespace PowersourceUIToolkit
{
    public static class ViewModelLocator
    {
        private static List<IViewModel> _ViewModelCache;

        static ViewModelLocator()
        {
            _ViewModelCache = new List<IViewModel>();
        }

        public static T GetViewModel<T>() where T : IViewModel
        {
            return (T)GetViewModel(typeof(T));
        }

        public static IViewModel GetViewModel(Type typeOfViewModel)
        {
            IViewModel v = (IViewModel)IoC.GetInstance(typeOfViewModel, null);

            if (!_ViewModelCache.Contains(v))
                _ViewModelCache.Add(v);

            return v;
        }

        public static void DisposeViewModels(bool disposing)
        {
            if (disposing)
            {
                foreach (IViewModel v in _ViewModelCache)
                {
                    v.Dispose();
                }

                _ViewModelCache.Clear();
            }
        }
    }
}
